//
//  SettingViewController.m
//  AppleMarket
//
//  Created by yangy on 31/10/2018.
//  Copyright © 2018 szgsy. All rights reserved.
//

#import "SettingViewController.h"
// view
#import "SettingView.h"
#import "SettingViewModel.h"
// controller
#import "LoginViewController.h"
#import "CM_NavigationViewController.h"
#import "WYWebController.h"
// mode
#import "NewsModel.h"

@interface SettingViewController ()
@property (nonatomic, strong) SettingView *settingView;
@property (nonatomic, strong) SettingViewModel *viewModel;
@end

@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"系统设置";
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.settingView];
    [_settingView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    [self bindViewModel];
}

- (void) bindViewModel
{
    @weakify(self);
    [self.viewModel.cellClickSubhject subscribeNext:^(id  _Nullable x) {
        if([x integerValue] == 0){
            // 费率详情
        }else if([x integerValue] == 1){
            // 语言
        }else{
            // 关于我们
            NSString *filePath = [[NSBundle mainBundle] pathForResource:@"AboutUS" ofType:@"html" inDirectory:@"html"];
            NSString *htmlStr = [[NSString alloc] initWithData: [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:filePath]] encoding:NSUTF8StringEncoding];
            WYWebController *webVC = [WYWebController webControllerWithTitle:@"用户协议" content:htmlStr];
            [self bh_PushViewController:webVC hiddenTabbar:YES];
        }
    }];
    
    [self.viewModel.refershRateEndSubject subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        if([x isKindOfClass:[NewsModel class]]){
            NewsModel *n = (NewsModel*) x;
            WYWebController *webVC = [WYWebController webControllerWithTitle:n.DocTitle content:n.DocContent];
            [self bh_PushViewController:webVC hiddenTabbar:YES];
        }
    }];
    
    [self.viewModel.loginClickSubject subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        // 登录
        LoginViewController *loginVC = [[LoginViewController alloc] init];
        CM_NavigationViewController *rootVC = [[CM_NavigationViewController alloc] initWithRootViewController:loginVC];
        [self presentViewController:rootVC animated:YES completion:nil];
    }];
    
    [self.viewModel.loginoutSuccesSubject subscribeNext:^(id  _Nullable x) {
        // 退出登录成功
        [self.navigationController popToRootViewControllerAnimated:YES];
    }];
    
}

- (SettingView *)settingView
{
    if(!_settingView){
        _settingView = [[SettingView alloc] initWithFrame:self.view.bounds viewModel:self.viewModel];
    }
    return _settingView;
}

- (SettingViewModel *)viewModel
{
    if(!_viewModel){
        _viewModel = [[SettingViewModel alloc] init];
    }
    return _viewModel;
}

@end
